#!/bin/bash
#主机信息
name=$(hostname)
ip=$(/usr/sbin/ip a show | /usr/bin/grep ens | /usr/bin/grep inet | /usr/bin/awk '{print $2}' | /usr/bin/awk -F '/' '{print $1}' | /usr/bin/head -n 1)
date=$(/usr/bin/date '+%Y-%m-%d-%H:%M:%S')

#自定义接入钉钉函数
function SendMessageToDingding() {
    #钉钉机器人接口地址
    url="https://oapi.dingtalk.com/robot/send?access_token=e620e6dfb1fae6cdb3852c10153471986938d603d8b4f78fef5753049fcc7524"

    #推送到钉钉
    curl -XPOST -s -L $url -H "Content-Type:application/json" -H "charset:utf-8" -d "{
        \"msgtype\": \"text\",
        \"text\": {
            \"content\": \"主机名：$1\n主机IP: $2\n报错信息："内网与线上VPN断开，请处理！"\n巡检时间：$3\"
            }, 
        'at': {
            'atMobiles': [
                '13421830635'
            ], 
            'isAtAll': false
        }
    }"
}

ping 10.8.2.1 -c 3 >/dev/null

if [ $? != 0 ]; then
    #调用函数
    SendMessageToDingding $name $ip $date
fi
